//
//  ShowReplyViewController.m
//  demoapp
//
//  Created by MingmingSun on 15/9/20.
//  Copyright (c) 2015年 pingplusplus. All rights reserved.
//

#import "ShowReplyViewController.h"
#import "ShowCommitView.h"
#import "KGModal.h"
#import "tooles.h"
#import "PullingRefreshTableView.h"
#import "UIImageView+AFNetworking.h"

@interface ShowReplyViewController ()<UITableViewDataSource,UITableViewDelegate,PullingRefreshTableViewDelegate,ShowCommitDelegate>

@property(nonatomic,weak) IBOutlet PullingRefreshTableView *listView;
@property(assign,nonatomic) BOOL refreshing;
@property(assign,nonatomic) NSInteger page;

@end

@implementation ShowReplyViewController

@synthesize listView;
@synthesize refreshing = _refreshing;
@synthesize page = _page;
@synthesize replyDic = _replyDic;

- (instancetype)initWithReplyDic:(NSDictionary*)aDic{
    self = [super init];
    if(self){
        self.replyDic = aDic;
//        self.title = [aDic valueForKey:@"title"];
        self.title = @"评论详情";
    }
    return self;
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.navigationItem.leftBarButtonItem =
        [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrow_white"]
                                         style:UIBarButtonItemStyleDone
                                        target:self
                                        action:@selector(backPressed:)];
        self.navigationItem.rightBarButtonItem =
        [[UIBarButtonItem alloc] initWithTitle:@"回复"
                                         style:UIBarButtonItemStyleDone
                                        target:self
                                        action:@selector(rightBarPressed:)];
    }
    return self;
}

-(void)backPressed:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightBarPressed:(id)sender{
    ShowCommitView *timeView = [[ShowCommitView alloc] init];
    timeView.myDelegate = self;
    timeView.aReplyID = [[self.replyDic valueForKey:@"id"] integerValue];
    [[KGModal sharedInstance] showWithContentView:timeView andAnimated:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 0;
    _refreshing = YES;
    self.listView.pullingDelegate = self;
    [self.listView awakeWhenXIB:self.listView.frame];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//listReply

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 1;
        case 1:{
            NSArray *rreplys = [self.replyDic objectForKey:@"rreplys"];
            return rreplys.count;
        }
        default:
            return 0;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80.0f;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *inde;
    switch (indexPath.section) {
        case 0:{
            inde= @"ShowHeadID";
            cell = [tableView dequeueReusableCellWithIdentifier:inde];
            if (cell == nil) {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ShowHeadCell" owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }
            UILabel *lab1 = (UILabel*)[cell viewWithTag:1];
            UILabel *lab2 = (UILabel*)[cell viewWithTag:2];
            UILabel *lab3 = (UILabel*)[cell viewWithTag:3];
            UILabel *lab4 = (UILabel*)[cell viewWithTag:4];
            UILabel *lab5 = (UILabel*)[cell viewWithTag:5];
            UIImageView *img1 = (UIImageView*)[cell viewWithTag:99];
            lab2.text = [tooles replaceNullFromDic:self.replyDic withKeyArray:@[@"replyDate"]];
            lab3.text = [tooles replaceNullFromDic:self.replyDic withKeyArray:@[@"content"]];
            lab4.hidden = YES;
            lab5.hidden = YES;
            lab5.frame = CGRectMake(272, 60 , 40, 20);
            lab1.text = [tooles replaceNullFromDic:self.replyDic withKeyArray:@[@"customer",@"name"]];
            NSString *headImage = [[self.replyDic objectForKey:@"customer"] valueForKey:@"headImage"];
            if((![headImage isEqual:[NSNull null]])
               && headImage
               && headImage.length){
                [img1 setImageWithURL:[NSURL URLWithString:headImage] placeholderImage:[UIImage imageNamed:@"default_photo.png"]];
            }
        }
            break;
        case 1:{
            inde= @"ShowHeadID";
            cell = [tableView dequeueReusableCellWithIdentifier:inde];
            if (cell == nil) {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ShowHeadCell" owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }
            UILabel *lab1 = (UILabel*)[cell viewWithTag:1];
            UILabel *lab2 = (UILabel*)[cell viewWithTag:2];
            UILabel *lab3 = (UILabel*)[cell viewWithTag:3];
            UILabel *lab4 = (UILabel*)[cell viewWithTag:4];
            UILabel *lab5 = (UILabel*)[cell viewWithTag:5];
            lab5.hidden = YES;
            UIImageView *img1 = (UIImageView*)[cell viewWithTag:99];
            NSDictionary *replyDic = [[self.replyDic objectForKey:@"rreplys"] objectAtIndex:indexPath.row];
            lab2.text = [tooles replaceNullFromDic:replyDic withKeyArray:@[@"replyDate"]];
            lab3.text = [tooles replaceNullFromDic:replyDic withKeyArray:@[@"content"]];
            lab4.hidden = YES;
            lab5.frame = CGRectMake(272, 60 , 40, 20);
            lab1.text = [tooles replaceNullFromDic:replyDic withKeyArray:@[@"customer",@"name"]];
            NSString *headImage = [[replyDic objectForKey:@"customer"] valueForKey:@"headImage"];
            if((![headImage isEqual:[NSNull null]])
               && headImage
               && headImage.length){
                [img1 setImageWithURL:[NSURL URLWithString:headImage] placeholderImage:[UIImage imageNamed:@"default_photo.png"]];
            }
        }
            break;
        default:
            break;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    return;
}

#pragma mark - PullingRefreshTableViewDelegate
- (void)pullingTableViewDidStartRefreshing:(PullingRefreshTableView *)tableView{
    self.refreshing = YES;
    self.page = 0;
}

- (void)pullingTableViewDidStartLoading:(PullingRefreshTableView *)tableView{
    self.refreshing = NO;
    self.page++;
}

-(void)showPressed:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
    [self.myDelegate showPressed:nil];
}

#pragma mark - Scroll
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.listView tableViewDidScroll:scrollView];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//    [self.listView tableViewDidEndDragging:scrollView];
}

@end